﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net.Appender;
using Telerik.Web.UI;

public partial class Settings : BasePage
{
 
    public override string Icon
    {
        get
        {
            return "images/report_icon.png";
        }
    }
 


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        	Fill();
        }
    }
    

	protected void lbtnSave_Click(object sender, EventArgs e)
	{
		DateTime date = DateTime.Now;
		if (rbtnDate.Checked)
		{
			date = radDP_Date.SelectedDate == null
			                	? DateTime.Now
			                	: new DateTime(radDP_Date.SelectedDate.Value.Year,
			                	               radDP_Date.SelectedDate.Value.Month,
			                	               radDP_Date.SelectedDate.Value.Day,
			                	               Convert.ToInt32(txtHour.Text),
			                	               Convert.ToInt32(txtMinute.Text), 0);
		}

		if (rbtnDateNow.Checked)
			date = DateTime.Now;

		decimal frequency = Convert.ToDecimal(ddlFrequency.SelectedValue);
		
		string message;
		new DataLayer().SaveEmailDeliverySettings(PAConfiguration.ConnectionString, date, frequency, out message);
	}

	protected void lbtnCancel_Click(object sender, EventArgs e)
	{
		Fill();
	}

	protected override void Fill()
	{
		string message;
		var dt = new DataLayer().GetEmailDeliverySettings(PAConfiguration.ConnectionString, out message);

		if (dt.Rows.Count > 0)
		{
			DateTime nextDeliveryDate = (DateTime) dt.Rows[0]["NEXT_DELIV_DATE"];
			radDP_Date.SelectedDate = nextDeliveryDate;
			txtHour.Text = string.Format("{0:00}", nextDeliveryDate.Hour);
			txtMinute.Text = string.Format("{0:00}", nextDeliveryDate.Minute);

			ddlFrequency.SelectedValue = dt.Rows[0]["FREQUENCY"].ToString();
		}
	}
}